set scheme s2color

use "$project_path/data/4_regdata/regdata", clear

local bandwidths = "100 20 10 7.5 5 2.5"
local columns = `" "100" "20" "10" "7.5" "5" "2.5" "RD" "'

local variables_to_analyse = "std_diff_m1_growth_gdpc_penn mstd_diff_m1_inflation_imf mstd_diff_m1_unemployment_ilo std_diff_m1_trade_wb diff_m1_chix_econ std_diff_m1_educ_index std_diff_m1_income_index std_diff_m1_lifeexp_index std_diff_m1_hdi std_diff_m1_democracy_index diff_m1_general_chix"

foreach v in `variables_to_analyse' {
	
	local v_root = subinstr("`v'", "diff_m1_", "", .)
	local v_root = subinstr("`v_root'", "mstd_", "", .)
	local v_root = subinstr("`v_root'", "std_", "", .)

	matrix res_mat_`v_root' = J(7,3,.)
	matrix coln res_mat_`v_root' = tau ci_low ci_high 
	matrix rown res_mat_`v_root' = `columns'
	
	local row = 0

	//OLS with different bandwidths
	foreach bandwidth in `bandwidths' {
		local row = `row' + 1
		qui reg `v' treatment if abs(runvar)<=`bandwidth', level(90)
		matrix res_mat_`v_root'[`row',1] = r(table)["b", "treatment"]
		matrix res_mat_`v_root'[`row',2] = r(table)["ll", "treatment"]
		matrix res_mat_`v_root'[`row',3] = r(table)["ul", "treatment"]
	}
	
	//RDD
	local row = `row' + 1
	qui rdrobust `v' runvar, level(90) 
	matrix res_mat_`v_root'[`row',1] = e(tau_cl)
	matrix res_mat_`v_root'[`row',2] = e(ci_l_rb)
	matrix res_mat_`v_root'[`row',3] = e(ci_r_rb)

}

// Figure 7(a)
coefplot ///
(matrix(res_mat_chix_econ[.,1]), ci((res_mat_chix_econ[.,2] res_mat_chix_econ[.,3])) offset(-0.2) color(midblue) ciopts(lwidth(*2) lcolor(midblue) )) ///
(matrix(res_mat_growth_gdpc_penn[.,1]), ci((res_mat_growth_gdpc_penn[.,2] res_mat_growth_gdpc_penn[.,3])) offset(-0.1) color(dkgreen) ciopts(lwidth(*2) lcolor(dkgreen)) msymbol(O) mfcolor(white)) ///
(matrix(res_mat_inflation_imf[.,1]), ci((res_mat_inflation_imf[.,2] res_mat_inflation_imf[.,3])) offset(0) color(midgreen) ciopts(lwidth(*2) lcolor(midgreen)) msymbol(O) mfcolor(white)) ///
(matrix(res_mat_unemployment_ilo[.,1]), ci((res_mat_unemployment_ilo[.,2] res_mat_unemployment_ilo[.,3])) offset(0.1) color(sand) ciopts(lwidth(*2) lcolor(sand)) msymbol(O) mfcolor(white)) ///
(matrix(res_mat_trade_wb[.,1]), ci((res_mat_trade_wb[.,2] res_mat_trade_wb[.,3])) offset(0.2) color(erose) ciopts(lwidth(*2) lcolor(erose)) msymbol(O) mfcolor(white)) ///
, yline(0) vertical title("Economic performance", size(vlarge)) xlabel(,labsize(large)) ylabel(-0.2(0.2)0.8,labsize(large) angle(0)) xtitle(" " "OLS bandwidth     ", size(large)) graphregion(color(white))  ytitle("Treatment effect", size(large)) xline(6.5, lwidth(thick) lcolor(black%30)) legend(label(2 "Econ. perf.") label(4 "GDP growth") label(6 "(Minus) Inflation") label(8 "(Minus) Unemp.") label(10 "Trade") rows(2) size(large) region(col(white)))
graph export "$project_path/output/figures/Figure_7a.pdf", as(pdf) name("Graph") replace

// Figure 7(b)
coefplot ///
(matrix(res_mat_hdi[.,1]), ci((res_mat_hdi[.,2] res_mat_hdi[.,3])) offset(-0.2) color(midblue) ciopts(lwidth(*2) lcolor(midblue) )) ///
(matrix(res_mat_income_index[.,1]), ci((res_mat_income_index[.,2] res_mat_income_index[.,3])) offset(-0.1) color(dkgreen) ciopts(lwidth(*2) lcolor(dkgreen)) msymbol(O) mfcolor(white)) ///
(matrix(res_mat_lifeexp_index[.,1]), ci((res_mat_lifeexp_index[.,2] res_mat_lifeexp_index[.,3])) offset(0) color(midgreen) ciopts(lwidth(*2) lcolor(midgreen)) msymbol(O) mfcolor(white)) ///
(matrix(res_mat_educ_index[.,1]), ci((res_mat_educ_index[.,2] res_mat_educ_index[.,3])) offset(0.1) color(sand) ciopts(lwidth(*2) lcolor(sand)) msymbol(O) mfcolor(white)) ///
, yline(0) vertical title("Human Development Index", size(vlarge)) xlabel(,labsize(large)) ylabel(-0.2(0.2)0.8,labsize(large) angle(0)) xtitle(" " "OLS bandwidth     ", size(large)) graphregion(color(white))  ytitle("Treatment effect", size(large)) xline(6.5, lwidth(thick) lcolor(black%30)) legend(label(2 "HDI") label(4 "Income component") label(6 "Life expectancy component") label(8 "Education component") rows(2) size(large) region(col(white)))
graph export "$project_path/output/figures/Figure_7b.pdf", as(pdf) name("Graph") replace

// Figure 7(c)
coefplot ///
(matrix(res_mat_democracy_index[.,1]), ci((res_mat_democracy_index[.,2] res_mat_democracy_index[.,3])) offset(-0) color(midblue) ciopts(lwidth(*2) lcolor(midblue) )) ///
, yline(0) vertical title("Democracy", size(vlarge)) xlabel(,labsize(large)) ylabel(-0.1(0.1)0.5,labsize(large) angle(0)) xtitle(" " "OLS bandwidth     ", size(large)) graphregion(color(white))  ytitle("Treatment effect", size(large)) xline(6.5, lwidth(thick) lcolor(black%30)) legend(off)
graph export "$project_path/output/figures/Figure_7c.pdf", as(pdf) name("Graph") replace

// Figure 7(d)
coefplot ///
(matrix(res_mat_general_chix[.,1]), ci((res_mat_general_chix[.,2] res_mat_general_chix[.,3])) offset(-0) color(midblue) ciopts(lwidth(*2) lcolor(midblue) )) ///
, yline(0) vertical title("General index", size(vlarge)) xlabel(,labsize(large)) ylabel(-0.1(0.1)0.5,labsize(large) angle(0)) xtitle(" " "OLS bandwidth     ", size(large)) graphregion(color(white))  ytitle("Treatment effect", size(large)) xline(6.5, lwidth(thick) lcolor(black%30)) legend(off)
graph export "$project_path/output/figures/Figure_7d.pdf", as(pdf) name("Graph") replace

